﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace TianShiProject.Models.Common
{
    public class JsonDateConverter:JsonConverter<DateTime?>
    {
        public override bool HandleNull => true;
        public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            => (reader.GetString() != null ? DateTime.ParseExact(reader.GetString(), "yyyy-MM-dd", CultureInfo.InvariantCulture) : null);

        public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
        =>writer.WriteStringValue(value?.ToString("yyyy-MM-dd",CultureInfo.InvariantCulture));

    }
}
